<?php 
require_once('AuthenticationHandler.class.php');
class LDAPAuthenticationHandler extends  AuthenticationHandler 
{
    var $ldapServer;
    var $ldapPort;
    var $ldapBaseDN;
    var $ldapObject;
    function LDAPAuthenticationHandler($params)
    {

        

        // set default parameters
        $defparams             = array();
        $defparams['server']    = 'localhost';
        $defparams['port'] = 389;
        $defparams['basedn'] = NULL;

        // merge parameters
        $params = array_merge($defparams, $params);
        $this->ldapServer=$params['server']    ;
        $this->ldapPort=$params['port'] ;
        $this->ldapBaseDN =$params['basedn'];
        

    }
    
    function print_error(){
        printf("LDAP-Errno: %s<br>\n", $this->ldapObject->ldapErrno); 
        printf("LDAP-Error: %s<br>\n", $this->ldapObject->ldapError); 
    } 
    
    function login (&$controller,&$user){
        
        $this->ldapObject = & new ldap($this->ldapServer);        
        
        
        $credential =$user->getCredential();
        $u = $credential->getUsername();
        $p = $credential->getPassword();
        
        //connect        
        if ($this->ldapObject->connect()){
            //bind
            $dnb="cn=".$u.",".$this->ldapBaseDN;
            if ($this->ldapObject->bind($dnb, $p)){
                
                $ldapResult=$this->ldapObject->searchSubtree($this->ldapBaseDN, "(cn=".$u.")");
                if ($ldapResult){    
                    $ldapResultEntry= $ldapResult->firstEntry();
                    $fa=$ldapResultEntry->getFirstAttribute();
                    $av=$ldapResultEntry->getValues($fa);
                    $user->setAttribute(strtolower($fa),$av);
                    while (($a=$ldapResultEntry->getNextAttribute())){
                        $av=$ldapResultEntry->getValues($a);
                        $user->setAttribute(strtolower($a),$av);
                    }
                }else{
                    $this->print_error();
                }
                return true;
            }else{
                $this->print_error();
            }
        }else{
            $this->print_error();
        }
/*        
        $ds=ldap_connect( $this->ldapServer , $this->ldapPort);
        if (@ldap_bind( $ds, $dnb, $p )){
            $r = @ldap_search( $ds, $this->ldapBaseDN, "(cn=".$u.")" );
            $nr=@ldap_count_entries( $ds, $r);
            if ($nr>0){
                $result = @ldap_first_entry($ds, $r);
                $fa=@ldap_first_attribute($ds, $result,$ber);
                $av=@ldap_get_values($ds, $result,$fa);
                $user->setAttribute($fa,$av);
                while (($a=@ldap_next_attribute($ds, $result,$ber))){
                    $av=@ldap_get_values($ds, $result,$a);
                    $user->setAttribute($a,$av);
                }
                @ldap_close($ds); 
                return TRUE;
            }else{
            }
        }else{
            printf("LDAP-Errno: %s<br>\n", ldap_errno($ds)); 
            printf("LDAP-Error: %s<br>\n", ldap_error($ds)); 

            return false;
        }
                ldap_close($ds); 
*/                
        return false;
    }
    
    function logout (&$controller,&$user){
        exit;        
        
    }
    
    

}

?>